define([
        "core",
        "angular",
        "sanitize"
    ],
    function (core, angular, santize) {
        var menu = {}
        var menuLink = "menu-link";

        function prepareMenus(menus) {
            var html = "";
            for (var i in menus) {
                var menu = menus[i];
                if (menu.children != null && menu.children.length > 0) {
                    html += prepareFoldMenu(menu);
                } else {
                    html += prepareHrefMenu(menu);
                }
            }
            return html;
        };

        function prepareLabelMenu(menu) {
            var html = '<li class="header">' + menu.name + '</li>'
        };

        function prepareFoldMenu(menu) {
            var html = "";
            html += '<li class="treeview">';
            html += '<a href="#">' +
                '<i class="fa ' + menu.iconCss + '"></i> <span>' + menu.name + '</span>' +
                '<i class="fa fa-angle-left pull-right"></i>' +
                '</a>'

            html += '<ul class="treeview-menu" style="display: none;">'

            html += prepareMenus(menu.children);

            html += '</ul>'
            html += '</li>';

            return html;
        };

        function prepareHrefMenu(menu) {
            var html = "";
            html += '<li><a href="' + core.getUrl(menu.pageUrl) + '" class="' + menuLink + '" title="' + menu.name + '">' +
                '<i class="fa ' + menu.iconCss + '"></i><span>' + menu.name + '</span></a></li>'
            return html;
        };

        function prepareContentHeader(title) {
            var html = '<h1>' + title + '</h1>';
            html += '<ol class="breadcrumb">'
            html += '<li><a href="#"><i class="fa fa-dashboard"></i>首页</a></li>';
            html += '<li class="active">' + title + '</li>';
            html += '</ol>';
            return html;
        };


        /**
         * 不用
          */
        menu.initNavbarTopMenus = function (selector, topMenuDataUrl, sidebarSelector, baseSidebarMenuDataUrl) {
            $.ajax({
                url: core.getUrl(topMenuDataUrl),
                success: function (data) {
                    var html = '<ul class="nav navbar-nav">';
                    data.forEach(function (d) {
                        html += '<li><a href="' + data.pageUrl + '">'
                        html += d.name
                        html += '</a></li>';
                    })
                    html += '</ul>'
                    $(selector).html(html);

                    $(selector + " a").on("click", function (e) {
                        var elem = $(this);
                        var url = elem.data("url");
                        if (!url) {
                            url = elem.attr("href");
                            elem.data("url", url);
                            elem.attr("href", "#");
                        }
                        loadContent(url, prepareContentHeader(e.target.innerText));

                        menu.initMenus(sidebarSelector, baseSidebarMenuDataUrl);
                    })
                }
            })
        }

        menu.initMenus = function (selector, menuDataUrl, headerContent) {
            $.ajax({
                url: core.getUrl(menuDataUrl),
                success: function (data) {
                    var html = '<ul class="sidebar-menu">';
                    headerContent && (html += '<li class="header">' + headerContent + '</li>');
                    html += prepareMenus(data) + '</ul>';
                    $(selector).html(html);

                    core.linkOnclick("a." + menuLink, function (data) {
                        var scope = core.globalData("scope");
                        core.clearTimer();
                        if (scope) {
                            scope.$destroy && scope.$destroy();
                        }

                        var $injector = angular.injector(['ng']);
                        $injector.invoke(function ($compile, $rootScope, $sce) {
                            scope = $rootScope.$new();
                            scope.$sce = $sce;
                            $compile($("div .main")[0])(scope);
                            core.globalData("scope", scope);
                        });

                        return data;
                    })
                }
            })
        }

        return menu;
    }
)
